package com.jiangjiang.eshop.promotion.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.promotion.dao.PromotionActivityGoodsRelationMapper;
import com.jiangjiang.eshop.promotion.domain.PromotionActivityGoodsRelationEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName PromotionActivityGoodsRelationService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/29 9:35
 * @Version 1.0
 **/
@Service
public class PromotionActivityGoodsRelationService
        extends ServiceImpl<PromotionActivityGoodsRelationMapper, PromotionActivityGoodsRelationEntity> {
    /**
     * 根据商品id查询营销活动关联关系
     * @param goodsId 商品id
     * @return 查询结果
     */
    public List<PromotionActivityGoodsRelationEntity> listByGoodsId(Long goodsId) {
        LambdaQueryWrapper<PromotionActivityGoodsRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PromotionActivityGoodsRelationEntity::getGoodsId, goodsId)
                .or().eq(PromotionActivityGoodsRelationEntity::getGoodsId, -1);
        return this.list(wrapper);
    }

    /**
     * 根据营销活动id查询营销活动与商品的关联关系
     * @param promotionActivityId 营销活动id
     * @return 查询结果
     */
    public List<PromotionActivityGoodsRelationEntity> queryByPromotionActivityId(Long promotionActivityId) {
        LambdaQueryWrapper<PromotionActivityGoodsRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PromotionActivityGoodsRelationEntity::getPromotionActivityId, promotionActivityId);
        return this.list(wrapper);
    }

    /**
     * 根据营销活动id删除营销活动与商品的关联关系
     * @param promotionActivityId 营销活动id
     */
    public void removeByPromotionId(Long promotionActivityId) {
        LambdaQueryWrapper<PromotionActivityGoodsRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PromotionActivityGoodsRelationEntity::getPromotionActivityId, promotionActivityId);
        this.remove(wrapper);
    }
}
